import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

/*
 *  Finance.java : Finance class gets current pictures of three-month charts from web 
 * 					for the big countries
 *  @Author : BilCIS
 */

public class Finance extends JPanel 
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 4810980802270889609L;

	// Properties
	private JPanel contentPane;
	
	private String target;
	private String DowJones = "http://chart.finance.yahoo.com/z?s=^DJI&t=6m&q=l&l=on&z=s&p=m50,e200,v&a=p12,p&c=%5EDJI&region=eng&lang=eng-ENG";
	private String NASDAQ = "http://chart.finance.yahoo.com/z?s=^IXIC&t=6m&q=l&l=on&z=s&p=m50,e200,v&a=p12,p&c=%5EDJI&region=eng&lang=eng-ENG";
	private String S_P = "http://chart.finance.yahoo.com/z?s=^GSPC&t=6m&q=l&l=on&z=s&p=m50,e200,v&a=p12,p&c=%5EDJI&region=eng&lang=eng-ENG";
	private String Google = "http://chart.finance.yahoo.com/z?s=GOOG&t=6m&q=l&l=on&z=s&p=m50,e200,v&a=p12,p&c=%5EDJI&region=eng&lang=eng-ENG";
	private String Apple = "http://chart.finance.yahoo.com/z?s=AAPL&t=6m&q=l&l=on&z=s&p=m50,e200,v&a=p12,p&c=%5EDJI&region=eng&lang=eng-ENG";
	private String Microsoft = "http://chart.finance.yahoo.com/z?s=MSFT&t=6m&q=l&l=on&z=s&p=m50,e200,v&a=p12,p&c=%5EDJI&region=eng&lang=eng-ENG";
	private String Facebook = "http://chart.finance.yahoo.com/z?s=FB&t=6m&q=l&l=on&z=s&p=m50,e200,v&a=p12,p&c=%5EDJI&region=eng&lang=eng-ENG";
	private String Volkswagen = "http://chart.finance.yahoo.com/z?s=VOW.DE&t=6m&q=l&l=on&z=s&p=m50,e200,v&a=p12,p&c=%5EDJI&region=eng&lang=eng-ENG";
	private String Vodafone = "http://chart.finance.yahoo.com/z?s=VOD&t=6m&q=l&l=on&z=s&p=m50,e200,v&a=p12,p&c=%5EDJI&region=eng&lang=eng-ENG";
	private String Samsung = "http://chart.finance.yahoo.com/z?s=005930.KS&t=6m&q=l&l=on&z=s&p=m50,e200,v&a=p12,p&c=%5EDJI&region=eng&lang=eng-ENG";
	private String[] Companies = { "DowJones Industrial Average", "NASDAQ Composite", "S&P 500", "Google Incorporation", "Apple Incorporation", "Microsoft Corporation", "Facebook Incorporation", "Volkwagen",
								   "Vodafone Group", "Samsung Electronics"};
	private JComboBox<String> comboBox;
	
	private JLabel lblNewLabel;
	
	private ImageIcon image;
	
	private Employee emp;
	
	private Reader r;
	// Properties ending

	// Constructor
	public Finance( Employee emp )
	{	
		this.emp = emp;
		create();
		this.setVisible(true);
	}
	
	private void create()
	{
		contentPane = new JPanel();
		contentPane.setBounds(0, 0, 430, 430);
		contentPane.setLayout(null);
		this.add(contentPane);
		
		try {
			r = new Reader(DowJones, "abc");
			image = r.getPic();
		} catch (KeyManagementException e1) {
			e1.printStackTrace();
			JOptionPane.showMessageDialog(null, "No Internet Connection");
		} catch (NoSuchAlgorithmException e1) {
			e1.printStackTrace();
			JOptionPane.showMessageDialog(null, "No Internet Connection");
		} catch (IOException e1) {
			e1.printStackTrace();
			JOptionPane.showMessageDialog(null, "No Internet Connection");
		}
		if ( image == null)
			image = new ImageIcon( new File("finance.png").getAbsolutePath() );
		
		// Label part
		lblNewLabel = new JLabel();
		lblNewLabel.setBounds(10, 10, 414, 358);
		lblNewLabel.setIcon(image);
		contentPane.add(lblNewLabel);
		
		// ComoBox part
		comboBox = new JComboBox<String>(Companies);
		comboBox.addActionListener(new comboBox_Action());
		comboBox.setBounds(43, 380, 300, 40);
		comboBox.setToolTipText("Brings the company's last 3 months datas");
		contentPane.add(comboBox);
		
		//********************** Colors *********************//
		contentPane.setBackground(emp.getColor());
		
		//
		// Settings
		//	
		this.setSize(new Dimension(430, 430));
		this.setLayout(null);
	}
	
	// An action method for combobox,
	// Reads Companies' three-month charts
	public class comboBox_Action implements ActionListener
	{
		public void actionPerformed (ActionEvent e)
		{
			target = comboBox.getSelectedItem().toString();
			
			// DowJones Industrial Average
			if( target ==  "DowJones Industrial Average")
			{
				try {
					r = new Reader(DowJones, "abc");
					image = r.getPic();
				} catch (KeyManagementException e1) {
					image = new ImageIcon( new File("finance.png").getAbsolutePath() );
					e1.printStackTrace();
					JOptionPane.showMessageDialog(null, "No Internet Connection");
				} catch (NoSuchAlgorithmException e1) {
					image = new ImageIcon( new File("finance.png").getAbsolutePath() );
					e1.printStackTrace();
					JOptionPane.showMessageDialog(null, "No Internet Connection");
				} catch (IOException e1) {
					image = new ImageIcon( new File("finance.png").getAbsolutePath() );
					e1.printStackTrace();
					JOptionPane.showMessageDialog(null, "No Internet Connection");
				}
				
				lblNewLabel.setIcon(image);	
			}
			// NASDAQ Composite
			else if( target == "NASDAQ Composite" )
			{
				try {
					r = new Reader(NASDAQ, "abc");
					image = r.getPic();
				} catch (KeyManagementException e1) {
					image = new ImageIcon( new File("finance.png").getAbsolutePath() );
					e1.printStackTrace();
					JOptionPane.showMessageDialog(null, "No Internet Connection");
				} catch (NoSuchAlgorithmException e1) {
					image = new ImageIcon( new File("finance.png").getAbsolutePath() );
					e1.printStackTrace();
					JOptionPane.showMessageDialog(null, "No Internet Connection");
				} catch (IOException e1) {
					image = new ImageIcon( new File("finance.png").getAbsolutePath() );
					e1.printStackTrace();
					JOptionPane.showMessageDialog(null, "No Internet Connection");
				}
				
				lblNewLabel.setIcon(image);			
			}
			// S&P 500
			else if( target == "S&P 500" )
			{
				try {
					r = new Reader(S_P, "abc");
					image = r.getPic();
				} catch (KeyManagementException e1) {
					image = new ImageIcon( new File("finance.png").getAbsolutePath() );
					e1.printStackTrace();
					JOptionPane.showMessageDialog(null, "No Internet Connection");
				} catch (NoSuchAlgorithmException e1) {
					image = new ImageIcon( new File("finance.png").getAbsolutePath() );
					e1.printStackTrace();
					JOptionPane.showMessageDialog(null, "No Internet Connection");
				} catch (IOException e1) {
					image = new ImageIcon( new File("finance.png").getAbsolutePath() );
					e1.printStackTrace();
					JOptionPane.showMessageDialog(null, "No Internet Connection");
				}
				
				lblNewLabel.setIcon(image);				
			}
			// Google Incorporation
			else if( target == "Google Incorporation" )
			{
				try {
					r = new Reader(Google, "abc");
					image = r.getPic();
				} catch (KeyManagementException e1) {
					image = new ImageIcon( new File("finance.png").getAbsolutePath() );
					e1.printStackTrace();
					JOptionPane.showMessageDialog(null, "No Internet Connection");
				} catch (NoSuchAlgorithmException e1) {
					image = new ImageIcon( new File("finance.png").getAbsolutePath() );
					e1.printStackTrace();
					JOptionPane.showMessageDialog(null, "No Internet Connection");
				} catch (IOException e1) {
					image = new ImageIcon( new File("finance.png").getAbsolutePath() );
					e1.printStackTrace();
					JOptionPane.showMessageDialog(null, "No Internet Connection");
				}
				
				lblNewLabel.setIcon(image);				
			}
			// Apple Incorporation
			else if( target == "Apple Incorporation" )
			{
				try {
					r = new Reader(Apple, "abc");
					image = r.getPic();
				} catch (KeyManagementException e1) {
					image = new ImageIcon( new File("finance.png").getAbsolutePath() );
					e1.printStackTrace();
					JOptionPane.showMessageDialog(null, "No Internet Connection");
				} catch (NoSuchAlgorithmException e1) {
					image = new ImageIcon( new File("finance.png").getAbsolutePath() );
					e1.printStackTrace();
					JOptionPane.showMessageDialog(null, "No Internet Connection");
				} catch (IOException e1) {
					image = new ImageIcon( new File("finance.png").getAbsolutePath() );
					e1.printStackTrace();
					JOptionPane.showMessageDialog(null, "No Internet Connection");
				}
				
				lblNewLabel.setIcon(image);				
			}
			// Microsoft Corporation
			else if( target == "Microsoft Corporation" )
			{
				try {
					r = new Reader(Microsoft, "abc");
					image = r.getPic();
				} catch (KeyManagementException e1) {
					image = new ImageIcon( new File("finance.png").getAbsolutePath() );
					e1.printStackTrace();
					JOptionPane.showMessageDialog(null, "No Internet Connection");
				} catch (NoSuchAlgorithmException e1) {
					image = new ImageIcon( new File("finance.png").getAbsolutePath() );
					e1.printStackTrace();
					JOptionPane.showMessageDialog(null, "No Internet Connection");
				} catch (IOException e1) {
					image = new ImageIcon( new File("finance.png").getAbsolutePath() );
					e1.printStackTrace();
					JOptionPane.showMessageDialog(null, "No Internet Connection");
				}
				
				lblNewLabel.setIcon(image);				
			}
			// Facebook Incorporation
			else if( target == "Facebook Incorporation" )
			{
				try {
					r = new Reader(Facebook, "abc");
					image = r.getPic();
				} catch (KeyManagementException e1) {
					image = new ImageIcon( new File("finance.png").getAbsolutePath() );
					e1.printStackTrace();
					JOptionPane.showMessageDialog(null, "No Internet Connection");
				} catch (NoSuchAlgorithmException e1) {
					image = new ImageIcon( new File("finance.png").getAbsolutePath() );
					e1.printStackTrace();
					JOptionPane.showMessageDialog(null, "No Internet Connection");
				} catch (IOException e1) {
					image = new ImageIcon( new File("finance.png").getAbsolutePath() );
					e1.printStackTrace();
					JOptionPane.showMessageDialog(null, "No Internet Connection");
				}
				
				lblNewLabel.setIcon(image);	
			}
			// Volkwagen
			else if( target == "Volkwagen" )
			{
				try {
					r = new Reader(Volkswagen, "abc");
					image = r.getPic();
				} catch (KeyManagementException e1) {
					image = new ImageIcon( new File("finance.png").getAbsolutePath() );
					e1.printStackTrace();
					JOptionPane.showMessageDialog(null, "No Internet Connection");
				} catch (NoSuchAlgorithmException e1) {
					image = new ImageIcon( new File("finance.png").getAbsolutePath() );
					e1.printStackTrace();
					JOptionPane.showMessageDialog(null, "No Internet Connection");
				} catch (IOException e1) {
					image = new ImageIcon( new File("finance.png").getAbsolutePath() );
					e1.printStackTrace();
					JOptionPane.showMessageDialog(null, "No Internet Connection");
				}
				
				lblNewLabel.setIcon(image);	
			}
			// Vodafone Group
			else if( target == "Vodafone Group" )
			{
				try {
					r = new Reader(Vodafone, "abc");
					image = r.getPic();
				} catch (KeyManagementException e1) {
					image = new ImageIcon( new File("finance.png").getAbsolutePath() );
					e1.printStackTrace();
					JOptionPane.showMessageDialog(null, "No Internet Connection");
				} catch (NoSuchAlgorithmException e1) {
					image = new ImageIcon( new File("finance.png").getAbsolutePath() );
					e1.printStackTrace();
					JOptionPane.showMessageDialog(null, "No Internet Connection");
				} catch (IOException e1) {
					image = new ImageIcon( new File("finance.png").getAbsolutePath() );
					e1.printStackTrace();
					JOptionPane.showMessageDialog(null, "No Internet Connection");
				}
				
				lblNewLabel.setIcon(image);	
			}
			// Samsung Electronics
			else if( target == "Samsung Electronics" )
			{
				try {
					r = new Reader(Samsung, "abc");
					image = r.getPic();
				} catch (KeyManagementException e1) {
					image = new ImageIcon( new File("finance.png").getAbsolutePath() );
					e1.printStackTrace();
					JOptionPane.showMessageDialog(null, "No Internet Connection");
				} catch (NoSuchAlgorithmException e1) {
					image = new ImageIcon( new File("finance.png").getAbsolutePath() );
					e1.printStackTrace();
					JOptionPane.showMessageDialog(null, "No Internet Connection");
				} catch (IOException e1) {
					image = new ImageIcon( new File("finance.png").getAbsolutePath() );
					e1.printStackTrace();
					JOptionPane.showMessageDialog(null, "No Internet Connection");
				}
				
				lblNewLabel.setIcon(image);		
			}
		}
	}
}
